


document.addEventListener('DOMContentLoaded', function () {
    const crawlButton = document.getElementById('crawlButton');
    crawlButton.addEventListener('click', function () {
        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            chrome.scripting.executeScript({
                target: { tabId: tabs[0].id },
                function: crawlTableData
            });
        });
    });
});

function crawlTableData() {
    console.log("获取数据方法开始")
    const tables = document.querySelectorAll('table');
    const allData = [];

    tables.forEach(table => {
        const tableData = [];
        const rows = table.querySelectorAll('tr');
        rows.forEach(row => {
            const rowData = [];
            const cells = row.querySelectorAll('td, th');
            cells.forEach(cell => {
                rowData.push(cell.textContent.trim());
            });
            tableData.push(rowData);
        });
        allData.push(tableData);
    });

    console.log('当前页面表格数据:', allData);

    // 这里简单模拟翻页，实际需要根据具体网页的翻页逻辑修改
    const nextPageButton = document.querySelector('.next-page-button'); // 需根据实际网页修改类名
    if (nextPageButton) {
        nextPageButton.click();
        setTimeout(() => {
            crawlTableData();
        }, 2000); // 等待2秒后继续爬取下一页
    }
}
    